import 'package:flutter/material.dart';
import 'package:ljy_p_app/common/repositories/repos_item.dart';
import 'package:ljy_p_app/common/repositories/user_repository.dart';
import 'package:ljy_p_app/model/common_list_datatype.dart';
import 'package:ljy_p_app/style/lj_style.dart';
import 'package:ljy_p_app/widget/pull/lj_pull_load_widget.dart';
import 'package:ljy_p_app/widget/state/lj_list_state.dart';

/// 通用list
class CommonListPage extends StatefulWidget {
  final String? userName;

  final String? reposName;

  final String showType;

  final CommonListDatatype dataType;

  final String? title;

  const CommonListPage(this.title, this.showType, this.dataType, {super.key, this.userName, this.reposName});

  @override
  State<CommonListPage> createState() => _CommonListPageState();
}

class _CommonListPageState extends State<CommonListPage> with AutomaticKeepAliveClientMixin<CommonListPage>, LjListState<CommonListPage> {
  
  _renderItem(index) {
    if (pullLoadWidgetControl.dataList.isEmpty) {
      return null;
    }
    var data = pullLoadWidgetControl.dataList[index];
    switch (widget.showType) {
      case 'repositoryql':
      ReposViewModel reposViewModel = ReposViewModel.fromQL(data);
      return ReposItem(reposViewModel, onPressed: () {

      });
    }
  }

  _getDataLogic() async {
    return await UserRepository.getHistoryRequest(page);
  }

  /// 下拉刷新方法
  @override
  requestRefresh() async {
    return await _getDataLogic();
  }

  /// 上拉加载方法
  @override
  requestLoadMore() async {
    return await _getDataLogic();
  }
  
  @override
  bool get wantKeepAlive => true;

  @override
  bool get isRefreshFirst => true;

  @override
  bool get needHeader => false;
  
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title ?? "", maxLines: 1, overflow: TextOverflow.ellipsis,),
        backgroundColor: LJColors.primaryValue,
        foregroundColor: LJColors.textColorWhite,
      ),
      body: LjPullLoadWidget(pullLoadWidgetControl, (BuildContext context, int index) {
        return _renderItem(index);
      }, handleRefresh, onLoadMore, refreshKey: refreshIndicatorKey),
    );
  }
}